from django.shortcuts import render

import numpy as np
import pandas as pd
from  pyecharts import Bar,Pie

class ChipotelDate:

    def __init__(self):

        self.csv_name = r'E:\Project\date_analys\app\chipotle.csv'

        self.df = pd.read_csv(self.csv_name,sep = '\t')





    def bar(self, axis0, axix1, name = '分析结果',table_name = 'test1'):

        bar = Bar(name)

        bar.add('商家xx',axis0,axix1, is_label_show=True,is_datazoom_show=True)

        return bar
    def pie(self, axis0, axix1, name = '分析结果',table_name = 'test1'):

        pie = Pie(name)

        pie.add('商家xx',axis0,axix1,is_label_show=True)

        return pie

    def  how_many_are_sold_per_item(self):

        df1 = self.df.groupby('item_name').sum()

        df1.drop('order_id',axis= 1)

        return df1

#1、这段时间内餐厅的销售流水总额（5分）
    def many_count_Gross_sales(self):

        df1 = self.df.item_price.str.replace("$"," ")

        df2 = df1.astype('float64')

        df3 = df2.copy().sum()

        return df3
#2、这段时间内销售个数最多的商品 （5分）
    def Most_commodities(self):
        df1 = self.df.groupby('item_name').sum()
        #销售个数最多的商品df2
        df2 = df1.loc[:, 'quantity'].max()

        df3 = df1.sort_values('quantity', ascending=False).index[0]

        return df3
#3、这段时间内销售金额最多的商品（5分）
    def max_many_commdities(self):

        df1 = self.df.item_price.str.replace("$"," ").astype('float64')

        self.df['new_price'] = df1

        df2 = self.df
        df3 = df2.groupby('item_name').sum()
        df4 = df3.sort_values('new_price',ascending = False).index[0]
        return df4
#4、每种商品的销售个数，并做图（10分）
    def every_num_commodity_puter(self):
        df1 = self.df.groupby('item_name').sum()

        df2 = df1.loc[:,'quantity']

        df1.drop('order_id',axis= 1)

        return df1
        # from pyecharts import Bar
        #
        # arrt = df1.index
        #
        # vi = df2
        #
        # bar = Bar('每种商品的销售个数')
        #
        # bar.add('商家',arrt,vi,is_label_show=True,is_datazoom_show=True)
        #
        # return df1
#5、每种商品的销售金额，并做图（10分）
    def every_commodity_many_puter(self):

        df1 = self.df.item_price.str.replace("$", " ").astype('float64')

        self.df['new_price'] = df1

        df2 = self.df

        df1 = df2.groupby('item_name').sum()

        df3 = df1.drop(['order_id','quantity'],axis=1)

        return df3

        # from pyecharts import Bar
        #
        # arrt = df2.index
        #
        # v1 = df2.new_price
        #
        # bar = Bar('每种商品的销售金额')
        #
        # bar.add('商家',arrt,v1,is_datazoom_show=True,is_label_show=True)

#6、餐厅的产品的种类及对应的商品配料的价格，做出表格（10分）







#7、每种商品销售金额在流水总额中所占的比例，做圆饼图（10分）
    def every_product_hui_puter(self):

        df1 = self.df.item_price.str.replace("$", " ").astype('float64')

        self.df['new_price'] = df1

        df2 = self.df

        df3 = df2.groupby('item_name').sum()
        df4 = df3.drop(['order_id', 'quantity'], axis=1)

        return df4

        # from pyecharts import Pie
        #
        # vi = df2.loc[:,'new_price']
        #
        # arrt = df2.index
        #
        # pie = Pie('每种商品销售金额在流水总额中所占的比例')
        #
        # pie.add('本月账单',arrt,vi,is_label_show=True)


#8、每个定单金额，并做bar图（10分）
    def every_order_many_puter(self):
        df1 = self.df.item_price.str.replace("$", " ").astype('float64')

        self.df['new_price'] = df1

        df2 = self.df

        df3 = df2.groupby('order_id').sum()

        df4 = df3.drop('quantity',axis=1)


        return  df4

        # from pyecharts import Bar
        #
        # arrt = df2.order_id
        #
        # vi = df2.new_price
        #
        # bar = Bar('每个定单金额')
        #
        # bar.add('每笔单钱',arrt,vi,is_label_show=True,is_datazoom_show=True)
        #
        # return bar
#9、产品“Chicken Bowl”,选择最多的配料（5分）
    def max_choice_description(self):
        df1 = self.df.head(10)
        df2 = df1.drop(['order_id', 'quantity','item_price'], axis=1)
        df3 = df2.sort_values('choice_description',ascending = False).iloc[0]
        return df3






#10、产品“Chicken Bowl”每种配料选择的次数，及所占比例，并做圆饼图（10分）
    def max_choice_description_puter(self):
        df1 = self.df.head(10)
        df2 =df1.item_name == 'Chicken Bowl'
        df3 = df1[df2].drop(['order_id', 'quantity', 'item_price'], axis=1)
        df4 = df3.iloc[0]
        return df4



if __name__ == '__main__':
    x = ChipotelDate()
    info = x.max_choice_description()
    print(info)





